<!--
 * @Author: 一日看尽长安花
 * @since: 2019-10-12 15:43:18
 * @LastEditTime: 2019-11-30 22:50:15
 * @LastEditors: 一日看尽长安花
 * @Description:
 -->
<template>
  <div>
    <table-views
      :metedata="metedata"
      :tabledata.sync="tabledata"
      :loading="loading"
      :search-method="searchMethod"
      @pagination="pagination"
      @filter-search="filterSearch"
      @create-data="createData"
      @delete-data="deleteData"
      @update-data="updateData"
    >
      <!-- 往搜索栏中添加搜索条件 -->
      <template #filter-condition="{filterData:filterData}">
        <div class="filter-item-container">
          <el-form-item>
            <el-input
              v-model="filterData['test']"
              placeholder="test"
              :clearable="true"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
        </div>
      </template>
      <template #operation-btn-group>
        <el-button
          class="filter-item"
          style="margin-left: 10px;"
          type="primary"
          icon="el-icon-edit"
          size="mini"
        >
          测试
        </el-button>
      </template>
      <template #dialog-form-item="{dialogData:dialogData}">
        <el-form-item key="editorslotkey" label="编辑插槽" prop="test">
          <el-input
            v-model="dialogData['test']"
            placeholder="test"
            :clearable="true"
            style="width: 200px;"
            class="filter-item"
          />
        </el-form-item>
      </template>
    </table-views>
  </div>
</template>

<script>
import TableViews from '@/views/table';

export default {
  name: 'Demo2',
  components: { TableViews },
  props: {},
  data() {
    return {
      // 整个页面的数据
      metedata: {},
      tabledata: {
        data: [],
        total: 0
      },
      loading: true
    };
  },
  computed: {},
  mounted() {
    this.obtainMetedata();
    this.obtainData();
  },
  methods: {
    obtainMetedata() {
      this.loading = true;
      this.$axios
        .get('/tables/metedata')
        .then(result => {
          const { code, data } = { ...result };
          this.metedata = Object.assign({}, data);
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false;
        });
    },
    obtainData(queryParams) {
      this.loading = true;
      this.$axios
        .get('/tables', { params: queryParams })
        .then(result => {
          const { code, data } = { ...result };
          this.tabledata = Object.assign({}, data);
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false;
        });
    },
    pagination(queryParams) {
      this.obtainData(queryParams);
    },
    searchMethod(search, tableData) {
      return tableData.filter(
        d => !search || d.name.toLowerCase().includes(search.toLowerCase())
      );
    },
    filterSearch(queryParams) {
      this.obtainData(queryParams);
    },
    createData(dialogData) {
      this.$nextTick(() => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 2000
        });
      });
    },
    updateData(dialogData) {
      this.$nextTick(() => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 2000
        });
      });
    },
    deleteData(index, row) {
      this.$nextTick(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        });
      });
    }
  }
};
</script>
